package com.zjdiepu.www.biz.third.net;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.HttpRequestErrorType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.consts.ThirdService;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.HttpClient;
import com.zjdiepu.www.config.AliServiceProperty;
import com.zjdiepu.www.dao.third.JuheAreaMapper;
import com.zjdiepu.www.dao.third.JuheCityMapper;
import com.zjdiepu.www.dao.third.JuheProvinceMapper;
import com.zjdiepu.www.ibiz.third.net.IAreaService;
import com.zjdiepu.www.model.third.dto.JuheArea;
import com.zjdiepu.www.model.third.dto.JuheCity;
import com.zjdiepu.www.model.third.dto.JuheProvince;
import com.zjdiepu.www.model.third.response.ali.AliAreaDetailResponse;
import com.zjdiepu.www.model.third.response.ali.AreaDetail;
import com.zjdiepu.www.model.third.response.ali.AreaShowApiResBody;
import com.zjdiepu.www.util.ExceptionHelper;
/**
 * 项目名称：diepu-parent-biz <br/> 
 * 类名称：AliAreaServiceImpl <br/>  
 * 类描述： 查询地区<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年7月18日 下午1:35:48  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年7月18日 下午1:35:48   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Service
public class AliAreaServiceImpl extends BaseService implements IAreaService {
	@Resource
	protected HttpClient httpClient;
	@Resource
	private JuheProvinceMapper juheProvinceMapper;
	@Resource
	private JuheCityMapper juheCityMapper;
	@Resource
	private JuheAreaMapper juheAreaMapper;
	@Resource
	private AliServiceProperty aliServiceProperty;
	
	@Override
	public void saveAreaDetail(String parentId) {
		//由事务控制，异常删除的数据恢复
		juheAreaMapper.deleteAll();
		juheCityMapper.deleteAll();
		juheProvinceMapper.deleteAll();
		
		List<AreaDetail> provinces = getAreaByParent(parentId);
		
		if(CollectionUtils.isNotEmpty(provinces)) {
			List<JuheProvince> juheProvinces = CollectionInitUtil.emptyList();
			for(AreaDetail province : provinces) {
				JuheProvince juheProvince = new JuheProvince();
				juheProvince.initInsertData("aliyun", "阿里云市场");
				juheProvince.setRegionCode(province.getId().substring(0, 2));
				juheProvince.setAreaCode(province.getAreaCode());
				juheProvince.setZipCode(province.getZipCode());
				juheProvince.setProId(province.getProvinceId());
				juheProvince.setProName(province.getAreaName());
				juheProvince.setPinyin(province.getPinYin());
				juheProvince.setPin(province.getSimplePy());
				juheProvince.setLogo(null);
				juheProvinces.add(juheProvince); //省份
				
				List<AreaDetail> cities = getAreaByParent(province.getId());
				if(CollectionUtils.isNotEmpty(cities)) {
					List<JuheCity> juheCities = CollectionInitUtil.emptyList();
					for(AreaDetail city : cities) {
						JuheCity juheCity = new JuheCity();
						juheCity.initInsertData("aliyun", "阿里云市场");
						juheCity.setRegionCode(city.getId().substring(0, 4));
						juheCity.setProId(city.getProvinceId());
						juheCity.setAreaCode(city.getAreaCode());
						juheCity.setZipCode(city.getZipCode());
						juheCity.setCityId(city.getCityId());
						juheCity.setCityName(city.getAreaName());
						juheCity.setPinyin(city.getPinYin());
						juheCities.add(juheCity); //城市
						
						List<AreaDetail> areas = getAreaByParent(city.getId());
						if(CollectionUtils.isNotEmpty(areas)) {
							List<JuheArea> juheAreas = CollectionInitUtil.emptyList();
							for(AreaDetail area : areas) {
								JuheArea juheArea = new JuheArea();
								juheArea.initInsertData("aliyun", "阿里云市场");
								juheArea.setRegionCode(area.getId().substring(0, 6));
								juheArea.setCityId(area.getCityId());
								juheArea.setAreaCode(area.getAreaCode());
								juheArea.setZipCode(area.getZipCode());
								juheArea.setAreaId(area.getId());
								juheArea.setAreaName(area.getAreaName());
								juheArea.setPinyin(area.getPinYin());
								juheAreas.add(juheArea); //城市
							}
							if(CollectionUtils.isNotEmpty(juheAreas)) {
								juheAreaMapper.insertBatch(juheAreas);
							}
						}
					}
					if(CollectionUtils.isNotEmpty(juheCities)) {
						juheCityMapper.insertBatch(juheCities);
					}
				}
			}
			if(CollectionUtils.isNotEmpty(juheProvinces)) {
				juheProvinceMapper.deleteAll();
				juheProvinceMapper.insertBatch(juheProvinces);
			}
		}
		
	}
	private List<AreaDetail> getAreaByParent(String parentId) {
		List<AreaDetail> areaDetails = CollectionInitUtil.emptyList();
		String responseJson = "";
		try{
			if (StringUtils.isBlank(parentId)) {
				ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
			}
			Map<String, String> paramMap = getEmptyParamMap();
			paramMap.put("parentId", parentId);
			responseJson = httpClient.get(ThirdService.ALI_AREA_DETAIL, paramMap, getAliCommonHeaderMap(aliServiceProperty.getAppCode()));
			if (StringUtils.isBlank(responseJson)) {
				ExceptionHelper.thow(HttpRequestErrorType.RESPONSE_IS_EMPTY);
			}
			AliAreaDetailResponse response = JSON.parseObject(responseJson, AliAreaDetailResponse.class);
			if (null == response || !SystemConsts.RETURN_SUCCESS_CODE.equals(response.getShowapiResCode())) {
				ExceptionHelper.thow(HttpRequestErrorType.REQUEST_API_ERROR);
			}
			AreaShowApiResBody showapiResBody = response.getShowapiResBody();
			if (null == showapiResBody || !SystemConsts.RETURN_SUCCESS_CODE.equals(showapiResBody.getRetCode())) {
				ExceptionHelper.thow(HttpRequestErrorType.REQUEST_API_BUSSINESS_ERROR);
			}
			
			Boolean flag = showapiResBody.getFlag();
			if (null != flag && flag) {
				areaDetails = showapiResBody.getData();
			}
		} catch (Exception e) {
			logger.info("responseJson:" + responseJson + ",parentId:" + parentId);
		}
		return areaDetails;
	}
	
}

















